<?php
// +----------------------------------------------------------------------
// | TorrentsAction.class.php - MyPT
// +----------------------------------------------------------------------
// | Copyleft (c) 2010 By Asxzy All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Asxzy <asxzy@163.com>
// +----------------------------------------------------------------------
class TorrentsAction extends CommonAction{	
	// 首页跳转
	public function index(){
		$this->redirect(__APP__);
	}
	
	// 查看指定ID的种子
    public function view(){
    	if(empty($_GET['id'])){
        	$this->error('没有指定ID,请返回');
		}else{
			$torrents= M('Torrents');
			$data = $torrents->find($_GET['id']);
			if ($data){
				$torrents->setInc('views','id='.$_GET['id'],1);// views++
				$data = formatTorrents($data);
				$this->assign('torrents',$data);
				$this->display();
			}else{
				$this->error('没有找到对应的种子,请返回');
			}
		}
    }
    
    // 上传种子
    public function upload(){
    	if(empty($_POST)){
    		$this->display();
    	}else{
    		unset($_SESSION['__hash__']);// 取消hash验证
    		$torrents = D('Torrents');
    		$data = $torrents->create();
	    	if (!$data){
				$this->error($torrents->getError());
			}else{
				import("ORG.Net.UploadFile");
				$upload = new UploadFile(); // 实例化上传类
				$upload->maxSize  = 3145728 ; // 设置附件上传大小
				$upload->allowExts  =  array('torrent'); // 设置附件上传类型
				$upload->savePath =  C('UPLOAD_PATH').C('TORRENTS_PATH'); // 设置附件上传目录
				$upload->saveRule = 'com_create_guid';// 设置附件自动重命名
				if(!$upload->upload()) { // 上传错误提示错误信息
					$this->error($upload->getErrorMsg());
				}else{ // 上传成功获取上传文件信息
					$filename =  $upload->getUploadFileInfo();
				}
				// 开始处理种子文件
				$data['filename']=$filename[0]['name'];
				$data['save_as'] = $filename[0]["savename"];
				import("@.ParseTorrent.Parsetorrent");
				$TorrentInfo = ParseTorrent(C('UPLOAD_PATH').C('TORRENTS_PATH').$data['save_as']);
				// 准备数据
				if (isset($TorrentInfo['error'])){
					$this->error($TorrentInfo['error']);
				}
				$data['status'] = 'confirmed';
				$data['owner'] = $_SESSION[C('USER_AUTH_KEY')];
				$data['size'] = $TorrentInfo['size'];
				$data['info_hash'] = $TorrentInfo['infohash'];
				$data['numfiles'] = $TorrentInfo['numfiles'];
				$data['private'] = $TorrentInfo['private'];
				// 写入数据库
				if($torrents->add($data)){
					$this->success('发布成功');
				}else{
					unlink(C('UPLOAD_PATH').C('TORRENTS_PATH').$data['save_as']);
					$this->error($torrents->geterror());
				}
			}
    	}
    }
	// 编辑种子
    public function edit(){
    	if(empty($_POST)){
    		if(!isset($_GET['id'])) $this->error('没有指定ID,请返回');
    		$torrents = M('Torrents');
    		$data = $torrents->find($_GET['id']);
    		if(!data) $this->error('没有找到相应的种子,请返回');
    		$this->assign('torrents',$data);
    		$this->display();
    	}else{
    		unset($_SESSION['__hash__']);// 取消hash验证
    		$torrents = D('Torrents');
    		$data = $torrents->create();
	    	if (!$data){
				$this->error($torrents->getError());
			}else{
				if($data['uploads']){
					$save_as = $data["save_as"];
					import("ORG.Net.UploadFile");
					$upload = new UploadFile(); // 实例化上传类
					$upload->maxSize  = 3145728 ; // 设置附件上传大小
					$upload->allowExts  =  array('torrent'); // 设置附件上传类型
					$upload->savePath =  C('UPLOAD_PATH').C('TORRENTS_PATH'); // 设置附件上传目录
					$upload->saveRule = 'com_create_guid';// 设置附件自动重命名
					if(!$upload->upload()) { // 上传错误提示错误信息
						$this->error($upload->getErrorMsg());
					}else{ // 上传成功获取上传文件信息
						$filename =  $upload->getUploadFileInfo();
					}
					// 开始处理种子文件
					$data['filename']=$filename[0]['name'];
					$data['save_as'] = $filename[0]["savename"];
					import("@.ParseTorrent.Parsetorrent");
					$TorrentInfo = ParseTorrent(C('UPLOAD_PATH').C('TORRENTS_PATH').$data['save_as']);
					// 准备数据
					if (isset($TorrentInfo['error'])){
						$this->error($TorrentInfo['error']);
					}
					$data['status'] = 'confirmed';
					$data['owner'] = $_SESSION[C('USER_AUTH_KEY')];
					$data['size'] = $TorrentInfo['size'];
					$data['info_hash'] = $TorrentInfo['infohash'];
					$data['numfiles'] = $TorrentInfo['numfiles'];
					$data['private'] = $TorrentInfo['private'];
				}
				// 写入数据库
				if($torrents->save($data)){
					unlink(C('UPLOAD_PATH').C('TORRENTS_PATH').$save_as);
					$this->success('修改成功');
				}elseif(isset($data['uploads'])){
					unlink(C('UPLOAD_PATH').C('TORRENTS_PATH').$data['save_as']);
					$this->error($torrents->getError());
				}else{
					$this->error($torrents->getError());
				}
			}
    	}
    }
    // 下载
    public function download(){
    	if(empty($_GET['id'])){
        	$this->error('没有指定ID,请返回');
		}else{
			require_once("backend/bittorrent.php");
			$torrents = M('torrents');
			$row = $torrents->find($_GET['id']);
			$fn = C('UPLOAD_PATH').C('TORRENTS_PATH').$row["save_as"];
			require_once("backend/benc.php");
			$dict = bdec_file($fn, (1024*1024));
			$dict['value']['announce']['value'] = C('ANNOUNCE_URL')."?passkey=".$_SESSION["passkey"];
			$dict['value']['announce']['string'] = strlen($dict['value']['announce']['value']).":".$dict['value']['announce']['value'];
			$dict['value']['announce']['strlen'] = strlen($dict['value']['announce']['string']);
			
			header('Content-Disposition: attachment; filename="'.$row['name'].'.torrent"');
			header("Content-Type: application/x-bittorrent");
			$torrents->setInc('hits','id='.$_GET['id'],1);
			print(benc($dict));
		}
    }
}
?>